package delphorm.entite.questionnaire;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Question {

	String intitule;
	String consigne;
	Integer place;
	Integer maxReponses;
	Integer minReponses;
	Boolean ordonnee;
	List<Reponse> reponses;
	Long id;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Question()
	{
		reponses = new ArrayList<Reponse>();
	}
	
	public String getConsigne() {
		return consigne;
	}
	public void setConsigne(String consigne) {
		this.consigne = consigne;
	}
	public String getIntitule() {
		return intitule;
	}
	public void setIntitule(String intitule) {
		this.intitule = intitule;
	}
	public Integer getMaxReponses() {
		return maxReponses;
	}
	public void setMaxReponses(Integer maxReponses) {
		this.maxReponses = maxReponses;
	}
	public Integer getMinReponses() {
		return minReponses;
	}
	public void setMinReponses(Integer minReponses) {
		this.minReponses = minReponses;
	}
	public Boolean getOrdonnee() {
		return ordonnee;
	}
	public void setOrdonnee(Boolean ordonnee) {
		this.ordonnee = ordonnee;
	}
	public List<Reponse> getReponses() {
		return reponses;
	}
	public void setReponses(List<Reponse> reponses) {
		this.reponses = reponses;
	}
	
//	cette méthode retourne la réponse correspondant àu code
	public Reponse getReponse(Integer codeReponse)
	{
		//les réponses ne sont pas forcément dans l'ordre de leur identifiant dans l'ArrayList
		//qui les contient, il faut donc parcourir une par une les réponses
		Iterator itReponses = getReponses().iterator();
		while(itReponses.hasNext())
		{
			Reponse reponse = (Reponse)itReponses.next();
			if(reponse.getCode().equals(codeReponse))
			{
				return reponse;
			}
		}
		return null;
	}
	
	public Reponse supprimerReponse(Integer codeReponse)
	{
		Reponse reponse = getReponse(codeReponse);
		if(reponse == null)
			return null;
		getReponses().remove(reponse);
		return reponse;
	}

	public Integer getPlace() {
		return place;
	}

	public void setPlace(Integer place) {
		this.place = place;
	}

	public void echangerReponses(Integer id1, Integer id2) {
		Reponse temp = (Reponse)getReponses().get(id1.intValue());
		Reponse R2 = (Reponse)getReponses().get(id2.intValue());
		getReponses().set(id1.intValue(), R2);
		getReponses().set(id2.intValue(),temp);
	}
}
